﻿using SqlSugar;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace BLL
{
    public class SugarBaseBLL<T> : DbContext where T : class, new()
    {



        // 1.查询

        /// <summary>
        ///  适用于页面搜索 追加条件 分页排序查询。。 list.add(  new ConditionalModel() { ConditionalType = ConditionalType.Equal, FieldName = "userip", FieldValue = "211.23.23.3" });
        /// </summary>
        /// <param name="conditionalList"></param>
        /// <param name="page"></param>
        /// <returns></returns>
        public List<T> GetPageListBySql(string sql, PageModel page)
        {
           
            return Db.SqlQueryable<T>(sql).ToPageList(page.PageIndex,page.PageSize);
        }

        public virtual T GetById(dynamic id)
        {
            return new DbSet<T>(Db).GetById(id);
        }
        public List<T> GetList()
        {
            return new DbSet<T>(Db).GetList();

        }
        public List<T> GetList(Expression<Func<T, bool>> whereExpression)
        {
            return new DbSet<T>(Db).GetList(whereExpression);
        }

        /// <summary>
        ///  适用于页面搜索 追加条件 分页排序查询。。 list.add(  new ConditionalModel() { ConditionalType = ConditionalType.Equal, FieldName = "userip", FieldValue = "211.23.23.3" });
        /// </summary>
        /// <param name="conditionalList"></param>
        /// <param name="page"></param>
        /// <returns></returns>
        public List<T> GetList(List<IConditionalModel> conditionalList)
        {
            return new DbSet<T>(Db).GetList(conditionalList);
        }

        public T GetSingle(Expression<Func<T, bool>> whereExpression)
        {
            if (new DbSet<T>(Db).IsAny(whereExpression))
            {
                return new DbSet<T>(Db).GetSingle(whereExpression);
            }
            else
            {
                return null;
            }
          
        }

        public List<T> GetPageList(Expression<Func<T, bool>> whereExpression, PageModel page)
        {
            var result = new DbSet<T>(Db).GetPageList(whereExpression, page);
            return result;
        }

        /// <summary>
        /// 分页查询，加排序. 例如    bll.GetPageList(u => u.username == "姓名", pg,b=> b.id,OrderByType.Desc);
        /// </summary>
        /// <param name="whereExpression"></param>
        /// <param name="page"></param>
        /// <param name="orderByExpression"></param>
        /// <param name="orderByType"></param>
        /// <returns></returns>
        public List<T> GetPageList(Expression<Func<T, bool>> whereExpression, PageModel page, Expression<Func<T, object>> orderByExpression = null, OrderByType orderByType = OrderByType.Asc)
        {
            return new DbSet<T>(Db).GetPageList(whereExpression, page, orderByExpression, orderByType);
        }

        /// <summary>
        ///  适用于页面搜索 追加条件 分页排序查询。。 list.add(  new ConditionalModel() { ConditionalType = ConditionalType.Equal, FieldName = "userip", FieldValue = "211.23.23.3" });
        /// </summary>
        /// <param name="conditionalList"></param>
        /// <param name="page"></param>
        /// <returns></returns>
        public List<T> GetPageList(List<IConditionalModel> conditionalList, PageModel page)
        {
            return new DbSet<T>(Db).GetPageList(conditionalList, page);
        }

        /// <summary>
        ///  适用于页面搜索 追加条件 分页排序查询。 list.add(  new ConditionalModel() { ConditionalType = ConditionalType.Equal, FieldName = "userip", FieldValue = "211.23.23.3" });
        /// </summary>
        /// <param name="conditionalList"></param>
        /// <param name="page"></param>
        /// <param name="orderByExpression"></param>
        /// <param name="orderByType"></param>
        /// <returns></returns>
        public List<T> GetPageList(List<IConditionalModel> conditionalList, PageModel page, Expression<Func<T, object>> orderByExpression = null, OrderByType orderByType = OrderByType.Asc)
        {
            return new DbSet<T>(Db).GetPageList(conditionalList, page, orderByExpression, orderByType);
        }

        /// <summary>
        /// 条件是否匹配到
        /// </summary>
        /// <param name="whereExpression"></param>
        /// <returns></returns>
        public bool IsAny(Expression<Func<T, bool>> whereExpression)
        {
            return new DbSet<T>(Db).IsAny(whereExpression);
        }

        public int Count(Expression<Func<T, bool>> whereExpression)
        {
            return new DbSet<T>(Db).Count(whereExpression);
        }


        public bool Insert(T insertObj)
        {
            return new DbSet<T>(Db).Insert(insertObj);
        }

        /// <summary>
        /// 新增,并返回主键
        /// </summary>
        /// <param name="insertObj"></param>
        /// <returns></returns>
        public int InsertReturnIdentity(T insertObj)
        {
            return new DbSet<T>(Db).InsertReturnIdentity(insertObj);
        }
        public bool InsertRange(T[] insertObjs)
        {
            return new DbSet<T>(Db).InsertRange(insertObjs);
        }
        public bool InsertRange(List<T> insertObjs)
        {
            return new DbSet<T>(Db).InsertRange(insertObjs);
        }
        // 3.0 更新


        /// <summary>
        ///主键要有值， 主键是更新条件
        /// </summary>
        /// <param name="updateObj">对象</param>
        /// <returns></returns>
        public bool Update(T updateObj)
        {
            return new DbSet<T>(Db).Update(updateObj);
        }

        /// <summary>
        /// 更新数组
        /// </summary>
        /// <param name="updateObjs"></param>
        /// <returns></returns>
        public bool UpdateRange(T[] updateObjs)
        {
            return new DbSet<T>(Db).UpdateRange(updateObjs);
        }

        /// <summary>
        /// 根据条件 更新对象所有字段，(model,u=> u.nickName== "张飞11")
        /// </summary>
        /// <param name="updateObj">model</param>
        /// <param name="whereExpression"> 条件 u=> u.nickName== "张飞</param>
        /// <returns></returns>
        public bool Update(T updateObj, Expression<Func<T, bool>> whereExpression)
        {
            return new DbSet<T>(Db).Update(updateObj, whereExpression);
        }


        /// <summary>
        /// 根据条件 更新传入的字段， (u=>new NewsComment { Comment= "要更新的列" }),b=>  b.newsid==42
        /// </summary>
        /// <param name="columns"> (u=>new NewsComment { Comment= "要更新的列" })</param>
        /// <param name="whereExpression">b=>  b.Comment=="更新条件"</param>
        /// <returns></returns>
        public bool Update(Expression<Func<T, T>> columns, Expression<Func<T, bool>> whereExpression)
        {
            return new DbSet<T>(Db).Update(columns, whereExpression);
        }


        /// <summary>
        /// 根据对象的主键ID删除，传对象。 对象必须包含ID
        /// </summary>
        /// <param name="deleteObj"></param>
        /// <returns></returns>
        public bool Delete(T deleteObj)
        {
            return new DbSet<T>(Db).Delete(deleteObj);
        }

        /// <summary>
        /// 根据 lambda表达式 条件删除
        /// </summary>
        /// <param name="whereExpression"> u=> u.Comment== "哈哈3"</param>
        /// <returns>bool</returns>
        public bool Delete(Expression<Func<T, bool>> whereExpression)
        {
            return new DbSet<T>(Db).Delete(whereExpression);
        }

        /// <summary>
        /// 根据主键ID删除，传ID数字
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool DeleteById(dynamic id)
        {
            return new DbSet<T>(Db).DeleteById(id);
        }

        /// <summary>
        /// 根据ID数组删除.
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public bool DeleteByIds(dynamic[] ids)
        {
            return new DbSet<T>(Db).DeleteByIds(ids);
        }

        /// <summary>
        /// 复杂的查询用SQL
        /// </summary>
        /// <param name="sql"></param>
        /// <param name="parameters"></param>
        /// <returns></returns>
        public DataTable GetTableBySQL(string sql, object parameters)
        {
            return new DbSet<T>(Db).GetTableBySQL(sql, parameters);
        }


        /// <summary>
        ///  SQL查询单个
        /// </summary>
        /// <param name="sql"></param>
        /// <param name="parameters"></param>
        /// <returns></returns>
        public object GetScalarBySQL(string sql, object parameters)
        {
            return new DbSet<T>(Db).GetScalarBySQL(sql, parameters);

        }

        /// <summary>
        /// 执行SQL
        /// </summary>
        /// <param name="sql"></param>
        /// <param name="parameters"></param>
        /// <returns></returns>
        public int ExecuteCommand(string sql, object parameters)
        {
            return new DbSet<T>(Db).ExecuteCommand(sql, parameters);
        }


        /// <summary>
        /// 根据条件更新数据, 不更新字段,区分大小写
        /// </summary>
        /// <param name="updateObj"></param>
        /// <param name="whereExpression"></param>
        /// <param name="filename"></param>
        /// <returns></returns>
        public bool UpdateNoFiled(T updateObj, Expression<Func<T, bool>> whereExpression,string filename)
        {
            return new DbSet<T>(Db).UpdateNoFiled(updateObj, whereExpression, filename);
        }

        /// <summary>
        ///  根据条件更新数据, 不更新传入的字段， 区分大小写
        /// </summary>
        /// <param name="updateObj"></param>
        /// <param name="whereExpression"></param>
        /// <returns></returns>
        public bool UpdateNoFiled(T updateObj, string ignoreName)
        {
            return new DbSet<T>(Db).UpdateNoFiled(updateObj, ignoreName);
        }

        /// <summary>
        /// 根据条件更新, 不更新 传入的集合列字段， 区分大小写
        /// </summary>
        /// <param name="updateObj"></param> 
        /// <returns></returns>
        public bool UpdateNoList(T updateObj, List<string> list)
        {
            return new DbSet<T>(Db).UpdateNoList(updateObj, list);
        }

        /// <summary>
        /// 根据条件更新,  不更新 传入的集合列字段， 区分大小写
        /// </summary>
        /// <param name="updateObj"></param>
        /// <param name="whereExpression"></param>
        /// <returns></returns>
        public bool NoList(T updateObj, Expression<Func<T, bool>> whereExpression, List<string> list)
        {
            return new DbSet<T>(Db).UpdateNoList(updateObj, whereExpression, list);
        }


    }


}
